package cn.lzj.controller.User;

import cn.lzj.Utils.ReturnInfoUtils;
import cn.lzj.controller.base.indexController;
import cn.lzj.pojo.*;
import cn.lzj.service.User.CartService;
import cn.lzj.service.User.ProductService;
import cn.lzj.service.User.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @Resource
    private ProductService productService;

    @Resource
    private cn.lzj.controller.base.indexController indexController;

    //加入购物车
    @GetMapping("/shoppingInfoJoinCart/{productId}")
    public String shoppingInfoJoinCart(@PathVariable("productId") Integer productId,
                                       Model model,
                                       HttpServletRequest request

    ){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ProductBase product = productService.getProductById(productId);

        CartItem cartItem = new CartItem();
        cartItem.setCartItemNum(1);
        cartItem.setCartItemPrice(product.getProductPrice());
        cartItem.setUser(user);
        cartItem.setProduct(product);

        Cart cart = user.getCart();

        //生成购物车记录
        boolean success = cartService.addProductToCart(cartItem,cart);
        return ReturnInfoUtils.returnInfo(success);
    }


    @GetMapping("/showProduct")
    public String showProduct(HttpServletRequest request,Model model){

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        Cart newcart = cartService.getCart(user);
        Double totalMoney = newcart.getTotalMoney();
        System.out.println(totalMoney);

        model.addAttribute("cartInfo",newcart);
        return "/cart";
    }

    @GetMapping("/payall")
    public String payAll(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Cart cart = cartService.getCart(user);
        user.setCart(cart);

        int a = 0;
        PayTable payTable = new PayTable();
        Date now = new Date();
        int year = now.getYear();
        int month = now.getMonth();
        int day = now.getDay();
        int hour = now.getHours();
        int min = now.getMinutes();
        int sec = now.getSeconds();
        payTable.setPayNo(UUID.randomUUID().toString()+"_"+year+month+day+hour+min+sec);


        payTable.setPayTotalPrice(user.getCart().getTotalMoney());

        payTable.setPayDate(now);

        payTable.setOrderUser(user);

        payTable.setPayStatus(0);

        cartService.addPayTableBean(payTable);

        user.setCart(user.getCart());


//        model.addAttribute("user",user);

        session.setAttribute("user",user);
        return indexController.getProductByPage(1,user.getUserId(),
                "","","","",model,request);
    }


    @GetMapping("/delCartItem/{cartItemId}")
    private String delCartItem(@PathVariable("cartItemId") Integer cartItemId,
                               HttpServletRequest request,
                               Model model
            ){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Cart cart = cartService.getCart(user);
        user.setCart(cart);

        CartItem cartItem = cartService.getCartItemById(cartItemId);

        Cart newCart = cartService.delCartItem(cartItem, user.getCart());
        model.addAttribute("cartInfo",newCart);
        return "/cart";
    }



    @GetMapping("/editCartItem/{cartItemId}/{num}")
    private String editCartItem(HttpServletRequest request,
                                @PathVariable("cartItemId") Integer cartItemId,
                                @PathVariable("num") Integer num,
                                Model model
                                ){

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        CartItem cartItem = cartService.getCartItemById(cartItemId);
        cartItem.setCartItemNum(num);
        cartService.editCartItem(cartItem);


        Cart newCart = cartService.getCart(user);

        model.addAttribute("cartInfo",newCart);
        return "/cart";
    }

    @GetMapping("/clearCart")
    private String clearCart(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User User = (User) session.getAttribute("user");
        cartService.clearCart(User);

        Cart newCart = cartService.getCart(User);
        model.addAttribute("cartInfo",newCart);

        return "/cart";
    }

}
